/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lx_easyfinace;

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Administrator
 */
 public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {

    public TableCellTextAreaRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // 计算当下行的最佳高度
        int maxPreferredHeight = 0;
        for (int i = 0; i < table.getColumnCount(); i++) {
            Object o = table.getValueAt(row, i);
            setText("" + o);
            int w = table.getColumnModel().getColumn(column).getWidth();
            this.setSize(w, 0);
            int h = (int) getPreferredSize().getHeight();
            maxPreferredHeight = Math.max(maxPreferredHeight, h);
        }

        if (table.getRowHeight(row) != maxPreferredHeight && column == 4) // 少了这行则处理器瞎忙
        {
            table.setRowHeight(row, maxPreferredHeight);
        }

        setText(value == null ? "" : value.toString());
//        setAlignmentX(0.5f);
//        setAlignmentY(0.5f);
        return this;
    }
}
